package com.example.zyz.demo_sql_grid_list_notification_dialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Created by zyz on 15/11/18.
 */
public class add_dbactivity extends AppCompatActivity implements View.OnClickListener{
    private TextView name_textview;
    private TextView age_textview;
    private TextView sex_textview;
    private Button button_add;
    private Button button_display;
    private Button button_delete;
    private Button button_return;



    private SQLiteDatabase db;
    private TextView delete_textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_adddata);
        name_textview= (TextView) findViewById(R.id.editText_name);
        age_textview= (TextView) findViewById(R.id.editText2_age);
        sex_textview= (TextView) findViewById(R.id.editText3_sex);
        button_add= (Button) findViewById(R.id.add_button);
        delete_textview = (TextView) findViewById(R.id.editText_delete);
        button_display= (Button) findViewById(R.id.button_display);
        button_delete= (Button) findViewById(R.id.button_delete);
        button_return = (Button) findViewById(R.id.b_add_returnmain);

        button_add.setOnClickListener(this);
        button_delete.setOnClickListener(this);
        button_display.setOnClickListener(this);
        button_return.setOnClickListener(this);
        int xx=88;

   }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        open_new_db opendb=new open_new_db(add_dbactivity.this,"sqltest.db",null,1);
        SQLiteDatabase db=opendb.getWritableDatabase();
        switch (v.getId())
        {
            case R.id.add_button:
                db.execSQL("insert into stutb(name,sex,age)values('"+name_textview.getText() +"','"+sex_textview.getText()+"','"+age_textview.getText()+"')");
                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_delete:
                if (delete_textview.getText()!="")
                {
                    db.execSQL("delete from stutb where _id ='"+delete_textview.getText()+"'");
                    Toast.makeText(this,"删除成功，已删除" +delete_textview.getText(),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(this,"请输入要删除的Id",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button_display:
                Cursor c= db.rawQuery("select * from stutb",null);
                if (c!=null)
                {
                    String[] cols=c.getColumnNames();
                    while(c.moveToNext())
                    {
                        for (String ColumnName:cols)
                        {
                            Log.i("info", ColumnName + ":" + c.getString(c.getColumnIndex(ColumnName)));
                        }
                    }
                    c.close();
                }
                break;
            case R.id.b_add_returnmain:
//                Intent intent =new Intent();
//                intent.setClass(this,MainActivity.class);
//                startActivity(intent);
                this.finish();
                break;
        }


        db.close();
        int i=0;
    }
}
